package com.ebt.dl.util;

import static org.junit.Assert.assertEquals;

import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.ebt.dl.domain.Credential;

public class AuthenticateHelper {

	public static String getToken(int port, String userName, String password) {
		String api = "http://localhost:" + port + "/tokens";

		Credential credential = new Credential();
		credential.setUserId(userName);
		credential.setPassword(password);

		RestTemplate restTemplate = new RestTemplate();
		@SuppressWarnings("rawtypes")
		ResponseEntity<Map> entity = restTemplate.postForEntity(api, credential, Map.class);

		System.out.println("----------------------------");
		System.out.println("Status code: " + entity.getStatusCode());
		System.out.println("Status body: " + entity.getBody());
		assertEquals(HttpStatus.CREATED, entity.getStatusCode());

		return (String) entity.getBody().get("token");
	}
}
